package com.raidpoints.client;

import java.util.ArrayList;
import java.util.HashSet;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;

public class EventSignupBox extends DialogBox {
	
	ArrayList<character> charlist;
	HashSet<character> charsel;
	RaidPoints rp;
	event evt;
	PointsPage pp;
	private Label helpLabel;
	ScrollPanel span;
	VerticalPanel charpan;
	
	public EventSignupBox(RaidPoints mrp, event mevt){
		this(mrp,mevt,null);
	}
	
	/**
	 * @wbp.parser.constructor
	 */
	public EventSignupBox(RaidPoints mrp,event mevt,PointsPage mpp) {
		setHTML("Event Signup");
		rp = mrp;
		evt = mevt;
		pp = mpp;
		charsel = new HashSet<character>();
		
		if(evt.split > 0){
			for(event e : rp.dataProvider.getList()){
				if(e.eid == evt.split){
					evt = e;
					break;
				}
			}
		}
		
		LayoutPanel layoutPanel = new LayoutPanel();
		setWidget(layoutPanel);
		layoutPanel.setSize("274px", "355px");
		
		Label nameLabel = new Label("");
		layoutPanel.add(nameLabel);
		layoutPanel.setWidgetLeftWidth(nameLabel, 30.0, Unit.PX, 170.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(nameLabel, 13.0, Unit.PX, 16.0, Unit.PX);
		
		Label noteLabel = new Label("");
		layoutPanel.add(noteLabel);
		layoutPanel.setWidgetLeftWidth(noteLabel, 30.0, Unit.PX, 204.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(noteLabel, 46.0, Unit.PX, 18.0, Unit.PX);
		
		
		if(evt.game.equals("")){
			nameLabel.setText(evt.note.split(":;:")[0] + " " + evt.starttime.getDate());
			noteLabel.setText(evt.note.split(":;:")[1]);
		}
		else{
			nameLabel.setText(evt.dest + " " + evt.starttime.getDate());
			noteLabel.setText(evt.note);
		}
		
		Button signupButton = new Button("Sign Up");
		layoutPanel.add(signupButton);
		layoutPanel.setWidgetLeftWidth(signupButton, 30.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(signupButton, 308.0, Unit.PX, 28.0, Unit.PX);
		
		Button cancelButton = new Button("Cancel");
		layoutPanel.add(cancelButton);
		layoutPanel.setWidgetLeftWidth(cancelButton, 142.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(cancelButton, 308.0, Unit.PX, 28.0, Unit.PX);
		
		final TextBox noteBox = new TextBox();
		layoutPanel.add(noteBox);
		noteBox.setWidth("200px");
		layoutPanel.setWidgetLeftWidth(noteBox, 30.0, Unit.PX, 235.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(noteBox, 262.0, Unit.PX, 32.0, Unit.PX);
		
		Label lblNote = new Label("Note:");
		layoutPanel.add(lblNote);
		layoutPanel.setWidgetLeftWidth(lblNote, 30.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblNote, 246.0, Unit.PX, 16.0, Unit.PX);
		
//		final ListBox listBox = new ListBox();
//		listBox.setMultipleSelect(true);
//		layoutPanel.add(listBox);
//		listBox.setWidth("200px");
//		layoutPanel.setWidgetLeftWidth(listBox, 40.0, Unit.PX, 214.0, Unit.PX);
//		layoutPanel.setWidgetTopHeight(listBox, 92.0, Unit.PX, 90.0, Unit.PX);
//		listBox.setVisibleItemCount(5);
		span = new ScrollPanel();
		charpan = new VerticalPanel();
		layoutPanel.add(span);
		layoutPanel.setWidgetLeftWidth(span, 12.0, Unit.PX, 251.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(span, 102.0, Unit.PX, 138.0, Unit.PX);
		span.add(charpan);
		
		helpLabel = new Label("Select/Unselect chars to signup/cancel");
		layoutPanel.add(helpLabel);
		layoutPanel.setWidgetLeftWidth(helpLabel, 12.0, Unit.PX, 251.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(helpLabel, 80.0, Unit.PX, 16.0, Unit.PX);
		
		AsyncCallback<ArrayList<character>> charCallback = new AsyncCallback<ArrayList<character>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ArrayList<character> result) {
				if(result == null) return;
				charlist = result;
				for(int i = 0; i < charlist.size(); i++){
					charpan.add(new CharSignup(charlist.get(i),charlist.get(i).slot != null));
					if(charlist.get(i).slot != null) charsel.add(charlist.get(i));
//					listBox.addItem(charlist.get(i).name);
//					if(charlist.get(i).slot != null) listBox.setItemSelected(i, true);
				}
			}
			
		};
		rp.rpsvc.GetEligableCharacters(evt, charCallback);
		
		cancelButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				hide();				
			}
		});
		
		final AsyncCallback<String> addCallback = new AsyncCallback<String>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(String result) {
				if(pp != null) pp.Refresh();
				if(rp.navmap.get(rp.accountItem) != null){
				 ((AccountPage) rp.navmap.get(rp.accountItem)).Refresh();
				}
				((RaidPoints.HasRedraw)rp.navmap.get(rp.eventItem)).Redraw();
			}
			
		};
		
		signupButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				ArrayList<character> tsend = new ArrayList<character>();
				tsend.addAll(charsel);
//				for(int i = 0; i < listBox.getItemCount(); i++){
//					if(listBox.isItemSelected(i)) tsend.add(charlist.get(i));
//				}
				//if(tsend.size() == 0) {Window.alert("Must select a character"); return;}
				evt.attend="signedup";
				rp.rpsvc.CharacterSignup(evt,tsend,noteBox.getText(),addCallback);
				hide();				
			}
		});
		
	}
	
	public class CharSignup extends Composite{
		HTML chardisp;
		character mc;
		boolean ic;
		
		CharSignup(character tmc, boolean tic){
			mc = tmc;
			chardisp = new HTML();
			initWidget(chardisp);
			ic = tic;
			DrawChar(ic);
			
			chardisp.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					if(!ic){
						ic = true;
						charsel.add(mc);
					}
					else{
						ic = false;
						charsel.remove(mc);
					}
					DrawChar(ic);
				}
			});
		}
		
		void DrawChar(boolean clicked){
			if(clicked) chardisp.setHTML("<table><tr><td><table width=\"20\" height=\"20\" style=\"border:solid;border-width:2px;cursor:pointer;\"><tr><td bgcolor=\"668CD9\"></td></tr></table></td><td bgcolor=\"668CD9\"><font size=\"3\" style=\"background-color:668CD9;\">" + mc.name + "</font></td></tr></table>");
			else chardisp.setHTML("<table><tr><td><table width=\"20\" height=\"20\" style=\"border:solid;border-width:2px;cursor:pointer;\"><tr><td></td></tr></table></td><td><font size=\"3\">" + mc.name + "</font></td></tr></table>");
		}
		//#668CD9
		//if(value.clicked) name = "<font size=\"3\" style=\"background-color: #00CC00;" + (value.canceled ? " outline:red solid 4px;" : "") +"\">" + name + "</font>";
	}
}
